﻿
using TianYuan.Core.Domain.Entities;
using System.Collections.Generic;
using Dapper;
using System;
using SqlSugar;

namespace TianYuan.Data.Dapper.Repositories
{
    /// <summary>
    /// 泛型仓储类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    public abstract class Repository<TEntity, TKey> : SimpleClient<TEntity>, IRepository<TEntity, TKey>
        where TEntity : class, IEntity<TKey>, new()
    {
        /// <summary>
        /// 上下文，在一个webrequest请求中实例唯一
        /// </summary>
        public IDbContext _Context;

        public Repository(IDbContext context) : base(context)
        {
            _Context = context;
        }
        /// <summary>
        /// 获取Sugar数据操作对象
        /// </summary>
        /// <returns></returns>
        public IDbContext ContextInstance()
        {
            return _Context;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filedname"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public IEnumerable<TEntity> GetByFiledName(string filedname, string value)
        {
            List<TEntity> list = _Context.Queryable<TEntity>()
                .Where(filedname + "=@NAME")
                .AddParameters(new { NAME = value })
                .Select<TEntity>("*").ToList(); //也可以Select<T>(“*”).ToList()返回实体集合
            return list;
        }
        
    }
    public class Repository<TEntity> : Repository<TEntity, string>, IRepository<TEntity>
        where TEntity : class, IEntity<string>, new()
    {
        public Repository(IDbContext dbContext)
            : base(dbContext)
        {
        }

    }
}